// components/z-form/zy-calendar.js
const {dayjs} = require("../../common/lib");
const formCom = require('./zy-group-behavior');

const DAY_IN_WEEK = 7;
const FORMAT_TYPE = 'YYYY-M-D'

/**
 * 
 * @param {dayjs} curMonth 
 */
function createMonth(curMonth) {
    let firstDay = curMonth.startOf('month');
    let year = curMonth.year();
    let month = curMonth.month() + 1;

    let endDay = firstDay.endOf('month')
    let firstDayOfWeek = firstDay.day();
    let pre = firstDayOfWeek - 1;
    let endDayOfWeek = endDay.day();
    let after = DAY_IN_WEEK - endDayOfWeek ;
    console.log('after', after);

    let days = firstDay.daysInMonth();

    let month1 = Array(days).fill(0).map((v, index) => {
        let isLast = index === days - 1;
        return {
            label: index + 1,
            value: index + 1,
            year: year,
            month: month,
            date: index + 1,
            pre: pre,
            after: after,
            isLast: isLast
        }
    });

    return month1
}

Component({
    behaviors: [formCom],
    options: {
        dynamicSlots: true, // 启用动态 slot
    },
    methods: {
        date_click(e) {
            let self = this;
            this.m_click(e, {
                handle() {
                    let current = self._getCurrent();

                    let day = dayjs(new Date(current.year, current.month - 1, current.date));
                    // console.log(day.format(FORMAT_TYPE));
                    self.triggerEvent('change', {
                       value: day.format(FORMAT_TYPE)
                    })
                }
            })
        }
    },
    lifetimes: {
        attached() {
            this.formItem = this.selectOwnerComponent();

            let now = dayjs();

            let rangeOfMonth = 2;
            let options = []

            for (let i = 0; i < rangeOfMonth; i++) {
                let curMonth = now.add(i, 'month')
                let monthObj = createMonth(curMonth);
                options = options.concat(monthObj)
            }

            // console.log(options);
    
            this.setLabels(options) 
        }
    }
})